package com.cnap.multiclouldmgmt.service.impl;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.enums.K8sStatusEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.*;
import com.cnap.multiclouldmgmt.model.k8sMonitorModels.SvcMonitorModel;
import com.cnap.multiclouldmgmt.model.vm.VmModel;
import com.cnap.multiclouldmgmt.repository.master.K8sClusterEventsMapper;
import com.cnap.multiclouldmgmt.service.K8sClusterService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.PrometheusService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.*;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.ContainerMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsList;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.*;
import io.fabric8.kubernetes.client.dsl.internal.PodMetricOperationsImpl;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static org.mockito.ArgumentMatchers.*;

class K8sClusterServiceImplTest extends MulticlouldmgmtApplicationTests {

    @MockBean
    private K8sClusterEventsMapper k8sClusterEventsMapper;

    @Autowired
    private K8sClusterService k8sClusterService;

    @MockBean
    private MultiCloudMgmtService multiCloudMgmtService;

    @MockBean
    private PrometheusService prometheusService;

    private K8sClusterBasicModel getMockBasicModel(){

        K8sClusterBasicModel basicModel = Mockito.mock(K8sClusterBasicModel.class);
        try {
            Mockito.when(
                    multiCloudMgmtService
                            .getK8sClientByCloudVendorAndName("aliyun","clustername"))
                    .thenReturn(basicModel);

            Mockito.when(multiCloudMgmtService.getK8sClientByIp("1.1.1.1"))
                    .thenReturn(basicModel);

            KubernetesClient k8sClient = Mockito.mock(KubernetesClient.class);
            Mockito.when(basicModel.getK8sClient()).thenReturn(k8sClient);
            Mockito.when(basicModel.getK8sClusterIp()).thenReturn("1.1.1.1");
            Mockito.when(basicModel.getK8sClusterLocation()).thenReturn("jiangsu");
            Mockito.when(basicModel.getK8sClusterName()).thenReturn("clustername");
            Mockito.when(basicModel.getCreateUser()).thenReturn("admin");
            Mockito.when(basicModel.getCloudVendor()).thenReturn("aliyun");
            Mockito.when(basicModel.getK8sStatus()).thenReturn(K8sStatusEnum.Running.getChineseDescribe());

            //添加一个master节点
            NonNamespaceOperation<Node, NodeList, Resource<Node>> nodes = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.nodes()).thenReturn(nodes);
            NodeList nodeList = Mockito.mock(NodeList.class);
            Mockito.when(nodes.list()).thenReturn(nodeList);
            Node node = new Node();
            node.setMetadata(new ObjectMeta());
            node.getMetadata().setLabels(new HashMap<>());
            node.getMetadata().getLabels().put("node-role.kubernetes.io/master", "master");
            Mockito.when(nodeList.getItems()).thenReturn(Collections.singletonList(node));


            //增加pod （任何namespace）
            MixedOperation<Pod, PodList, PodResource> k8sPodsReturn = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.pods()).thenReturn(k8sPodsReturn);
            AnyNamespaceOperation<Pod, PodList, PodResource> inAnyNamespace = Mockito.mock(AnyNamespaceOperation.class);
            Mockito.when(k8sPodsReturn.inAnyNamespace()).thenReturn(inAnyNamespace);
            PodList podList = Mockito.mock(PodList.class);
            Mockito.when(k8sPodsReturn.inAnyNamespace().list()).thenReturn(podList);
            List<Pod> pods = new ArrayList<>();
            Pod pod2 = new Pod();
            Pod pod1 = new Pod();
            pods.add(pod1);
            pods.add(pod2);
            pod1.setMetadata(new ObjectMeta());
            pod1.getMetadata().setName("podName");
            pod1.getMetadata().setNamespace("default");
            pod1.getMetadata().setCreationTimestamp("2022-01-01T11:11:11Z");
            PodSpec podSpec = new PodSpec();
            podSpec.setNodeName("nodename");
            pod1.setSpec(podSpec);
            PodStatus podStatus = new PodStatus();
            podStatus.setPhase(CnapCloudMgmtConstants.POD_STATUS_RUNNING);
            pod1.setStatus(podStatus);
            pod1.getStatus().setPodIP("2.2.2.1");
            pod1.getStatus().setContainerStatuses(new ArrayList<>());

            pod2.setMetadata(new ObjectMeta());
            pod2.getMetadata().setName("podName2");
            pod2.getMetadata().setNamespace("default");
            pod2.getMetadata().setCreationTimestamp("2022-01-01T11:11:11Z");
            PodSpec podSpec2 = new PodSpec();
            podSpec.setNodeName("nodename");
            pod2.setSpec(podSpec2);
            PodStatus podStatus2 = new PodStatus();
            podStatus.setPhase(CnapCloudMgmtConstants.POD_STATUS_RUNNING);
            pod2.setStatus(podStatus2);
            pod2.getStatus().setPodIP("2.2.2.2");
            pod2.getStatus().setContainerStatuses(new ArrayList<>());

            ContainerStatus containerStatus = new ContainerStatus();
            containerStatus.setName("containername");
            containerStatus.setImage("containerimage");
            containerStatus.setRestartCount(10);
            containerStatus.setState(new ContainerState());
            containerStatus.getState().setRunning(new ContainerStateRunning());
            pod1.getStatus().getContainerStatuses().add(containerStatus);

            ContainerStatus containerStatus1 = new ContainerStatus();
            containerStatus1.setName("containername11");
            containerStatus1.setImage("containerimage22");
            containerStatus1.setRestartCount(11);
            containerStatus1.setState(new ContainerState());
            containerStatus1.getState().setTerminated(new ContainerStateTerminated());
            pod1.getStatus().getContainerStatuses().add(containerStatus1);

            ContainerStatus containerStatus2 = new ContainerStatus();
            containerStatus2.setName("containername122");
            containerStatus2.setImage("containerimage2");
            containerStatus2.setRestartCount(12);
            containerStatus2.setState(new ContainerState());
            containerStatus2.getState().setWaiting(new ContainerStateWaiting());
            pod1.getStatus().getContainerStatuses().add(containerStatus2);

            pod1.getStatus().setInitContainerStatuses(new ArrayList<>());
            pod1.getStatus().getInitContainerStatuses().add(containerStatus);
            pod1.getStatus().getInitContainerStatuses().add(containerStatus1);
            pod1.getStatus().getInitContainerStatuses().add(containerStatus2);

            ContainerStatus containerStatus20 = new ContainerStatus();
            containerStatus20.setName("containername2");
            containerStatus20.setImage("containerimage2");
            containerStatus20.setRestartCount(10);
            containerStatus20.setState(new ContainerState());
            containerStatus20.getState().setRunning(new ContainerStateRunning());
            pod2.getStatus().getContainerStatuses().add(containerStatus20);

            pod2.getStatus().setInitContainerStatuses(new ArrayList<>());
            pod2.getStatus().getInitContainerStatuses().add(containerStatus20);

            pod1.getSpec().setContainers(new ArrayList<>());
            Container container1 = new Container();
            pod1.getSpec().getContainers().add(container1);

            container1.setResources(new ResourceRequirements());
            container1.getResources().setRequests(new HashMap<>());
            container1.getResources().getRequests().put("cpu",new Quantity());
            container1.getResources().getRequests().get("cpu").setFormat("m");
            container1.getResources().getRequests().get("cpu").setAmount("100");
            container1.getResources().getRequests().put("memory",new Quantity());
            container1.getResources().getRequests().get("memory").setFormat("Mi");
            container1.getResources().getRequests().get("memory").setAmount("100");

            container1.getResources().setLimits(new HashMap<>());
            container1.getResources().getLimits().put("cpu",new Quantity());
            container1.getResources().getLimits().get("cpu").setFormat("m");
            container1.getResources().getLimits().get("cpu").setAmount("100000");
            container1.getResources().getLimits().put("memory",new Quantity());
            container1.getResources().getLimits().get("memory").setFormat("Mi");
            container1.getResources().getLimits().get("memory").setAmount("100000");

            pod2.getSpec().setContainers(new ArrayList<>());
            Container container2 = new Container();
            pod2.getSpec().getContainers().add(container2);

            container2.setResources(new ResourceRequirements());
            container2.getResources().setRequests(new HashMap<>());
            container2.getResources().getRequests().put("cpu",new Quantity());
            container2.getResources().getRequests().get("cpu").setFormat("m");
            container2.getResources().getRequests().get("cpu").setAmount("100");
            container2.getResources().getRequests().put("memory",new Quantity());
            container2.getResources().getRequests().get("memory").setFormat("Mi");
            container2.getResources().getRequests().get("memory").setAmount("100");

            container2.getResources().setLimits(new HashMap<>());
            container2.getResources().getLimits().put("cpu",new Quantity());
            container2.getResources().getLimits().get("cpu").setFormat("m");
            container2.getResources().getLimits().get("cpu").setAmount("100000");
            container2.getResources().getLimits().put("memory",new Quantity());
            container2.getResources().getLimits().get("memory").setFormat("Mi");
            container2.getResources().getLimits().get("memory").setAmount("100000");

            //pod2就层层继承
            pod2.getMetadata().setOwnerReferences(new ArrayList<>());
            OwnerReference ownerReferenceReplicaset = new OwnerReference();
            ownerReferenceReplicaset.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_REPLICASET);
            ownerReferenceReplicaset.setName("replicaset");
            pod2.getMetadata().getOwnerReferences().add(ownerReferenceReplicaset);

            //增加工作负载
            ReplicaSet replicaSet = new ReplicaSet();
            replicaSet.setMetadata(new ObjectMeta());
            replicaSet.getMetadata().setName("replicaset");
            replicaSet.getMetadata().setNamespace("default");
            replicaSet.getMetadata().setOwnerReferences(new ArrayList<>());
            OwnerReference statefulsetOwner = new OwnerReference();
            statefulsetOwner.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_STATEFULSET);
            statefulsetOwner.setName("statefulset");
            StatefulSet statefulSet = new StatefulSet();
            statefulSet.setMetadata(new ObjectMeta());
            statefulSet.getMetadata().setNamespace("default");
            statefulSet.getMetadata().setName("statefulset");
            replicaSet.getMetadata().getOwnerReferences().add(statefulsetOwner);
            statefulSet.getMetadata().setOwnerReferences(new ArrayList<>());
            OwnerReference daemonsetOwner = new OwnerReference();
            daemonsetOwner.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DAEMONSET);
            daemonsetOwner.setName("daemonset");
            DaemonSet daemonSet = new DaemonSet();
            daemonSet.setMetadata(new ObjectMeta());
            daemonSet.getMetadata().setName("daemonset");
            daemonSet.getMetadata().setNamespace("default");
            statefulSet.getMetadata().getOwnerReferences().add(daemonsetOwner);
            statefulSet.setStatus(new StatefulSetStatus());
            statefulSet.getStatus().setReplicas(1);
            statefulSet.getStatus().setAvailableReplicas(1);
            statefulSet.getStatus().setCurrentReplicas(1);
            statefulSet.setSpec(new StatefulSetSpec());
            statefulSet.getSpec().setTemplate(new PodTemplateSpec());
            statefulSet.getSpec().getTemplate().setSpec(new PodSpec());
            statefulSet.getSpec().getTemplate().getSpec().setContainers(new ArrayList<>());
            Container containerState = new Container();
            containerState.setName("statful-container-name");
            containerState.setImage("statful-container-image");
            statefulSet.getSpec().getTemplate().getSpec().getContainers().add(containerState);
            statefulSet.getMetadata().setCreationTimestamp("2022-11-11T11:11:11");
            statefulSet.getSpec().getTemplate().setMetadata(new ObjectMeta());
            statefulSet.getSpec().getTemplate().getMetadata().setLabels(new HashMap<>());
            statefulSet.getSpec().getTemplate().getMetadata().getLabels()
                    .put("app","svc1");

            //pod1继承deployment
            pod1.getMetadata().setOwnerReferences(new ArrayList<>());
            OwnerReference deployOwner = new OwnerReference();
            pod1.getMetadata().getOwnerReferences().add(deployOwner);
            deployOwner.setName("deployment");
            daemonsetOwner.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DEPLOYMENT);
            Deployment deployment = new Deployment();
            deployment.setMetadata(new ObjectMeta());
            deployment.getMetadata().setName("deployment");
            deployment.getMetadata().setNamespace("default");
            deployment.setStatus(new DeploymentStatus());
            deployment.getStatus().setConditions(new ArrayList<>());
            DeploymentCondition deploymentCondition = new DeploymentCondition();
            deploymentCondition.setType("Available");
            deploymentCondition.setStatus("True");
            deployment.getStatus().getConditions().add(deploymentCondition);
            deployment.getStatus().setReplicas(1);
            deployment.getStatus().setAvailableReplicas(1);
            deployment.getMetadata().setCreationTimestamp("2022-11-11T11:11:11");
            deployment.setSpec(new DeploymentSpec());
            deployment.getSpec().setTemplate(new PodTemplateSpec());
            deployment.getSpec().getTemplate().setSpec(new PodSpec());
            deployment.getSpec().getTemplate().getSpec().setContainers(new ArrayList<>());
            Container container = new Container();
            container.setName("deploy-container-name");
            container.setImage("deploy-container-image");
            deployment.getSpec().getTemplate().getSpec().getContainers().add(container);
            deployment.getSpec().getTemplate().setMetadata(new ObjectMeta());
            deployment.getSpec().getTemplate().getMetadata().setLabels(new HashMap<>());
            deployment.getSpec().getTemplate().getMetadata().getLabels()
                    .put("app","svc1");


            AppsAPIGroupDSL apps = Mockito.mock(AppsAPIGroupDSL.class);
            Mockito.when(k8sClient.apps()).thenReturn(apps);
            //retun replicaset
            MixedOperation<ReplicaSet, ReplicaSetList, RollableScalableResource<ReplicaSet>>
                    resourceMixedOperation = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.apps().replicaSets()).thenReturn(resourceMixedOperation);
            NonNamespaceOperation<ReplicaSet, ReplicaSetList, RollableScalableResource<ReplicaSet>>
                    replicasetsNamespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().replicaSets()
                    .inNamespace("default")).thenReturn(replicasetsNamespace);
            RollableScalableResource<ReplicaSet> replicaSetRollableScalableResource =
                    Mockito.mock(RollableScalableResource.class);
            Mockito.when(k8sClient.apps().replicaSets()
                    .inNamespace("default").withName("replicaset"))
                    .thenReturn(replicaSetRollableScalableResource);
            Mockito.when(k8sClient.apps().replicaSets()
                    .inNamespace("default").withName("replicaset").get())
                    .thenReturn(replicaSet);
            //return statefulset
            MixedOperation<StatefulSet, StatefulSetList, RollableScalableResource<StatefulSet>>
                    statefulSetMixedOperation = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.apps().statefulSets()).thenReturn(statefulSetMixedOperation);
            NonNamespaceOperation<StatefulSet, StatefulSetList, RollableScalableResource<StatefulSet>>
                    statefulsetsNamespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().statefulSets()
                    .inNamespace("default")).thenReturn(statefulsetsNamespace);
            RollableScalableResource<StatefulSet> statefulSetRollableScalableResource =
                    Mockito.mock(RollableScalableResource.class);
            Mockito.when(k8sClient.apps().statefulSets()
                            .inNamespace("default").withName("statefulset"))
                    .thenReturn(statefulSetRollableScalableResource);
            Mockito.when(k8sClient.apps().statefulSets()
                            .inNamespace("default").withName("statefulset").get())
                    .thenReturn(statefulSet);
            Mockito.when(k8sClient.apps().statefulSets()
                            .inNamespace("default").withName("statefulset").scale(anyInt()))
                    .thenReturn(null);
            Mockito.when(k8sClient.apps().statefulSets()
                    .inNamespace("default").resource(any(StatefulSet.class)))
                    .thenReturn(statefulSetRollableScalableResource);
            Mockito.when(statefulSetRollableScalableResource.createOrReplace())
                    .thenReturn(new StatefulSet());
            //~~~~~~~~~~~~~~~~~~~
            AnyNamespaceOperation<StatefulSet, StatefulSetList, RollableScalableResource<StatefulSet>>
                    statfulsetAnyNs
                    = Mockito.mock(AnyNamespaceOperation.class);
            Mockito.when(k8sClient.apps().statefulSets().inAnyNamespace())
                    .thenReturn(statfulsetAnyNs);
            StatefulSetList statefulSetList = Mockito.mock(StatefulSetList.class);
            Mockito.when(k8sClient.apps().statefulSets().inAnyNamespace().list())
                    .thenReturn(statefulSetList);
            List<StatefulSet> statefulSets = new ArrayList<>();
            statefulSets.add(statefulSet);
            Mockito.when(k8sClient.apps().statefulSets().inAnyNamespace().list().getItems())
                    .thenReturn(statefulSets);
            NonNamespaceOperation<StatefulSet, StatefulSetList, RollableScalableResource<StatefulSet>>
                    nonNameStateOperation = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().statefulSets()
                    .inNamespace(anyString())).thenReturn(nonNameStateOperation);
            Mockito.when(k8sClient.apps().statefulSets()
                    .inNamespace(anyString()).list()).thenReturn(statefulSetList);
            Mockito.when(k8sClient.apps().statefulSets()
                    .inNamespace(anyString()).list().getItems()).thenReturn(statefulSets);
            //返回demoset
            MixedOperation<DaemonSet, DaemonSetList, Resource<DaemonSet>>
                    sDaemonSetMixedOperation = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.apps().daemonSets()).thenReturn(sDaemonSetMixedOperation);
            NonNamespaceOperation<DaemonSet, DaemonSetList, Resource<DaemonSet>>
                    daemonSetDaemonSetListResourceNonNamespaceOperation
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().daemonSets()
                    .inNamespace("default")).thenReturn(daemonSetDaemonSetListResourceNonNamespaceOperation);
            Resource<DaemonSet> sDaemonSetResource =
                    Mockito.mock(Resource.class);
            Mockito.when(k8sClient.apps().daemonSets()
                            .inNamespace("default").withName("daemonset"))
                    .thenReturn(sDaemonSetResource);
            Mockito.when(k8sClient.apps().daemonSets()
                            .inNamespace("default").withName("daemonset").get())
                    .thenReturn(daemonSet);
            //返回deployment
            MixedOperation<Deployment, DeploymentList, RollableScalableResource<Deployment>>
                    deploymentMixedOperation = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.apps().deployments()).thenReturn(deploymentMixedOperation);
            NonNamespaceOperation<Deployment, DeploymentList, RollableScalableResource<Deployment>>
                    deploymentNamespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().deployments()
                    .inNamespace("default")).thenReturn(deploymentNamespace);
            RollableScalableResource<Deployment> deploymentResource =
                    Mockito.mock(RollableScalableResource.class);
            Mockito.when(k8sClient.apps().deployments()
                            .inNamespace("default").withName("deployment"))
                    .thenReturn(deploymentResource);
            Mockito.when(k8sClient.apps().deployments()
                            .inNamespace("default").withName("deployment").get())
                    .thenReturn(deployment);
            Mockito.when(k8sClient.apps().deployments()
                            .inNamespace("default").withName("deployment").scale(anyInt()))
                    .thenReturn(null);
            Mockito.when(k8sClient.apps().deployments().inNamespace("default")
                    .resource(any(Deployment.class))).thenReturn(deploymentResource);
            Mockito.when(k8sClient.apps().deployments().inNamespace("default")
                    .resource(any(Deployment.class))).thenReturn(deploymentResource);
            Mockito.when(deploymentResource.createOrReplace()).thenReturn(new Deployment());
            AnyNamespaceOperation<Deployment, DeploymentList, RollableScalableResource<Deployment>>
                    deploymentDeploymentListRollableScalableResourceAnyNamespaceOperation
                    = Mockito.mock(AnyNamespaceOperation.class);
            Mockito.when(k8sClient.apps().deployments().inAnyNamespace())
                    .thenReturn(deploymentDeploymentListRollableScalableResourceAnyNamespaceOperation);
            DeploymentList deploymentList = Mockito.mock(DeploymentList.class);
            Mockito.when(k8sClient.apps().deployments().inAnyNamespace().list())
                    .thenReturn(deploymentList);
            List<Deployment> deployments = new ArrayList<>();
            deployments.add(deployment);
            Mockito.when(k8sClient.apps().deployments().inAnyNamespace().list().getItems())
                            .thenReturn(deployments);
            NonNamespaceOperation<Deployment, DeploymentList, RollableScalableResource<Deployment>>
                    deployNonName = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.apps().deployments().inNamespace(anyString()))
                    .thenReturn(deployNonName);
            Mockito.when(k8sClient.apps().deployments().inNamespace(anyString()).list())
                    .thenReturn(deploymentList);
            Mockito.when(k8sClient.apps().deployments().inNamespace(anyString()).list()
                            .getItems())
                    .thenReturn(deployments);
            Mockito.when(k8sClient.apps().deployments().inNamespace(anyString()).list()
                            .getItems())
                    .thenReturn(deployments);
            RollableScalableResource<Deployment> noThis = Mockito.mock(RollableScalableResource.class);
            Mockito.when(k8sClient.apps().deployments().inNamespace(anyString()).withName("nodep"))
                    .thenReturn(noThis);
            Mockito.when(noThis.get()).thenReturn(null);

            pod1.getMetadata().setLabels(new HashMap<>());
            pod1.getMetadata().getLabels().put("podlableKey1","podlabvalue1");
            pod1.getMetadata().getLabels().put("app","svc1");
            pod2.getMetadata().setLabels(new HashMap<>());
            pod2.getMetadata().getLabels().put("podlableKey2","podlabvalue2");
            pod2.getMetadata().getLabels().put("app","svc2");


            //pod 结束
            Mockito.when(k8sPodsReturn.inAnyNamespace().list().getItems()).thenReturn(pods);

            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //增加在一个固定namespace下的东西
            NonNamespaceOperation<Pod, PodList, PodResource> inNamespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sPodsReturn.inNamespace("default")).thenReturn(inNamespace);
            Mockito.when(k8sPodsReturn.inNamespace("default").list()).thenReturn(podList);
            Mockito.when(k8sPodsReturn.inNamespace("default").list().getItems()).thenReturn(pods);
            PodResource podResource = Mockito.mock(PodResource.class);
            Mockito.when(k8sPodsReturn.inNamespace("default").withName("delpod")).thenReturn(podResource);
            Mockito.when(k8sPodsReturn.inNamespace("default").withName("delpod").delete()).thenReturn(null);
            Mockito.when(k8sPodsReturn.inNamespace("default").withName("podname")).thenReturn(podResource);
            Mockito.when(k8sPodsReturn.inNamespace("default").withName("podname").get())
                    .thenReturn(pod1);



            //增加namespace
            NonNamespaceOperation<Namespace, NamespaceList, Resource<Namespace>> namespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.namespaces()).thenReturn(namespace);
            NamespaceList namespaceList = Mockito.mock(NamespaceList.class);
            Mockito.when(k8sClient.namespaces().list()).thenReturn(namespaceList);
            List<Namespace> namespaces = new ArrayList<>();
            Namespace mockNamespace = new Namespace();
            mockNamespace.setMetadata(new ObjectMeta());
            mockNamespace.getMetadata().setName("default");
            mockNamespace.getMetadata().setAnnotations(new HashMap<>());
            mockNamespace.getMetadata().getAnnotations().put(
                    CnapCloudMgmtConstants.CNAP_DESCRIBE,"describe"
            );
            mockNamespace.getMetadata().setCreationTimestamp("2021-11-11T20:20:20");
            namespaces.add(mockNamespace);
            Mockito.when(k8sClient.namespaces().list().getItems()).thenReturn(namespaces);
            Resource<Namespace> resNs = Mockito.mock(Resource.class);
            Mockito.when(k8sClient.namespaces().resource(any(Namespace.class))).thenReturn(resNs);
            Mockito.when(resNs.createOrReplace()).thenReturn(new Namespace());
            Mockito.when(k8sClient.namespaces().withName(anyString())).thenReturn(resNs);
            Mockito.when(k8sClient.namespaces().withName(anyString()).delete()).thenReturn(null);



            //增加 pod metirc
            MetricAPIGroupDSL mockTop = Mockito.mock(MetricAPIGroupDSL.class);
            Mockito.when(k8sClient.top()).thenReturn(mockTop);
            PodMetricOperation mcokpodTop = Mockito.mock(PodMetricOperation.class);
            Mockito.when(k8sClient.top().pods()).thenReturn(mcokpodTop);
            PodMetricOperation mcokpodTopWithNs = Mockito.mock(PodMetricOperation.class);
            Mockito.when(k8sClient.top().pods().inNamespace("default"))
                    .thenReturn(mcokpodTopWithNs);
            PodMetricsList mockPodMectrics = Mockito.mock(PodMetricsList.class);
            Mockito.when(k8sClient.top().pods().inNamespace("default").metrics())
                    .thenReturn(mockPodMectrics);
            List<PodMetrics> podMetrics = new ArrayList<>();
            PodMetrics podMetrics1 = new PodMetrics();
            PodMetrics podMetrics2 = new PodMetrics();
            podMetrics.add(podMetrics1);
            podMetrics.add(podMetrics2);

            podMetrics1.setMetadata(new ObjectMeta());
            podMetrics1.getMetadata().setName("podName");
            podMetrics1.getMetadata().setNamespace("default");
            podMetrics1.setContainers(new ArrayList<>());
            ContainerMetrics containerMetrics1 = new ContainerMetrics();
            podMetrics1.getContainers().add(containerMetrics1);
            containerMetrics1.setUsage(new HashMap<>());
            containerMetrics1.getUsage().put("cpu",new Quantity());
            containerMetrics1.getUsage().get("cpu").setAmount("10");
            containerMetrics1.getUsage().get("cpu").setFormat("m");
            containerMetrics1.getUsage().put("memory",new Quantity());
            containerMetrics1.getUsage().get("memory").setAmount("10");
            containerMetrics1.getUsage().get("memory").setFormat("Ki");

            podMetrics2.setMetadata(new ObjectMeta());
            podMetrics2.getMetadata().setName("podName2");
            podMetrics2.getMetadata().setNamespace("default");
            podMetrics2.setContainers(new ArrayList<>());
            ContainerMetrics containerMetrics2 = new ContainerMetrics();
            podMetrics2.getContainers().add(containerMetrics2);
            containerMetrics2.setUsage(new HashMap<>());
            containerMetrics2.getUsage().put("cpu",new Quantity());
            containerMetrics2.getUsage().get("cpu").setAmount("10");
            containerMetrics2.getUsage().get("cpu").setFormat("m");
            containerMetrics2.getUsage().put("memory",new Quantity());
            containerMetrics2.getUsage().get("memory").setAmount("10");
            containerMetrics2.getUsage().get("memory").setFormat("Ki");

            Mockito.when(k8sClient.top().pods().inNamespace("default").metrics().getItems())
                    .thenReturn(podMetrics);

            //node的信息
            NonNamespaceOperation<Node, NodeList, Resource<Node>> nodesInfo
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.nodes()).thenReturn(nodesInfo);
            NodeList nodeList1 = Mockito.mock(NodeList.class);
            Mockito.when(k8sClient.nodes().list()).thenReturn(nodeList1);
            List<Node> nodes1 = new ArrayList<>();
            node.getMetadata().setName("nodename");
            Node node2 = new Node();
            node.setMetadata(new ObjectMeta());
            node.getMetadata().setName("nodename2");
            nodes1.add(node);
            nodes1.add(node2);

            Mockito.when(k8sClient.nodes().list().getItems()).thenReturn(nodes1);
            //Mockito.when(k8sClient.nodes().list().getItems()).thenReturn(2);
            Resource<Node> resNode = Mockito.mock(Resource.class);
            Mockito.when(k8sClient.nodes().withName("nodename")).thenReturn(resNode);
            Mockito.when(k8sClient.nodes().withName("nodename").get()).thenReturn(node);
            Mockito.when(k8sClient.nodes().resource(any(Node.class))).thenReturn(resNode);
            Mockito.when(k8sClient.nodes().withName("nodename").delete()).thenReturn(null);


            node.setStatus(new NodeStatus());
            node.getStatus().setConditions(new ArrayList<>());
            NodeCondition nodeCondition = new NodeCondition();
            nodeCondition.setType("Ready");
            nodeCondition.setStatus("True");
            node.getStatus().getConditions().add(nodeCondition);
            node.getStatus().setAddresses(new ArrayList<>());
            NodeAddress nodeAddress = new NodeAddress();
            nodeAddress.setType("IP");
            nodeAddress.setAddress("172.1.1.1");
            node.getStatus().getAddresses().add(nodeAddress);
            node.getStatus().setCapacity(new HashMap<>());
            Quantity nodePodsQuantity = new Quantity();
            nodePodsQuantity.setAmount("110");
            node.getStatus().getCapacity().put("pods",nodePodsQuantity);

            node2.setStatus(new NodeStatus());
            node2.getStatus().setConditions(new ArrayList<>());
            NodeCondition nodeCondition2 = new NodeCondition();
            nodeCondition2.setType("Ready");
            nodeCondition2.setStatus("false");
            node2.getStatus().getConditions().add(nodeCondition2);

            node.getMetadata().setAnnotations(new HashMap<>());
            node.getMetadata().getAnnotations().put(CnapCloudMgmtConstants.CNAP_NODE_TYPE,
                    "vm");
            node.getMetadata().getAnnotations().put(CnapCloudMgmtConstants.CNAP_NODE_LOCATION,
                    "js");
            node.getMetadata().getAnnotations().put(CnapCloudMgmtConstants.CNAP_NODE_FRAMEWORK,
                    "x86");
            node.getMetadata().getAnnotations().put(CnapCloudMgmtConstants.CNAP_NODE_VMNAME,
                    "vmName");
            node.getMetadata().getLabels().put("labelkey","labelValue");

            //node metrics
            NodeMetricOperation nodeMetricOperation = Mockito.mock(NodeMetricOperation.class);
            Mockito.when(k8sClient.top().nodes()).thenReturn(nodeMetricOperation);
            NodeMetrics nodeMetrics = new NodeMetrics();
            Mockito.when(k8sClient.top().nodes().metrics("nodename")).thenReturn(nodeMetrics);
            nodeMetrics.setUsage(new HashMap<>());
            nodeMetrics.getUsage().put("cpu",new Quantity());
            nodeMetrics.getUsage().get("cpu").setAmount("30");
            nodeMetrics.getUsage().get("cpu").setFormat("m");
            nodeMetrics.getUsage().put("memory",new Quantity());
            nodeMetrics.getUsage().get("memory").setAmount("30");
            nodeMetrics.getUsage().get("memory").setFormat("Ki");
            nodeMetrics.getUsage().put("ephemeral-storage",new Quantity());
            nodeMetrics.getUsage().get("ephemeral-storage").setAmount("300000000000");
            //实际使用额度
            Mockito.when(prometheusService
                    .getNodeDiskFreeUnitB("1.1.1.1","172.1.1.1",null))
                    .thenReturn(3000000l);
            Mockito.when(prometheusService
                            .getNodeNetIOPS("1.1.1.1","172.1.1.1",null))
                    .thenReturn(1000l);
            node.getMetadata().setCreationTimestamp("2020-11-11T11:11:11");

            //svc 的相关
            MixedOperation<Service, ServiceList, ServiceResource<Service>> servicesOperation
                    = Mockito.mock(MixedOperation.class);
            Mockito.when(k8sClient.services()).thenReturn(servicesOperation);
            ServiceList serviceList = Mockito.mock(ServiceList.class);
            Mockito.when(k8sClient.services().list()).thenReturn(serviceList);
            List<Service> services = new ArrayList<>();
            Service svc = new Service();
            services.add(svc);
            svc.setMetadata(new ObjectMeta());
            svc.getMetadata().setName("svcname");
            svc.getMetadata().setNamespace("default");
            svc.setSpec(new ServiceSpec());
            svc.getSpec().setType("NodePort");
            svc.getSpec().setClusterIP("3.3.3.3");
            svc.getSpec().setPorts(new ArrayList<>());
            ServicePort svcPort01= new ServicePort();
            svcPort01.setPort(8080);
            svcPort01.setNodePort(30180);
            svcPort01.setProtocol("tcp");
            svc.getSpec().getPorts().add(svcPort01);
            svc.getSpec().setSelector(new HashMap<>());
            svc.getSpec().getSelector().put("app","svc1");
            svc.getMetadata().setCreationTimestamp("2022-02-02T11:11:11");

            Mockito.when(k8sClient.services().list().getItems()).thenReturn(services);
            AnyNamespaceOperation<Service,
                    ServiceList, ServiceResource<Service>>
                    anyNamespaceOperation = Mockito.mock(AnyNamespaceOperation.class);
            Mockito.when(k8sClient.services().inAnyNamespace()).thenReturn(anyNamespaceOperation);
            NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>>
                    svcInNamespace
                    = Mockito.mock(NonNamespaceOperation.class);
            Mockito.when(k8sClient.services().inNamespace("default")).thenReturn(svcInNamespace);
            Mockito.when(k8sClient.services().inAnyNamespace().list()).thenReturn(serviceList);
            Mockito.when(k8sClient.services().inNamespace("default").list()).thenReturn(serviceList);
            Mockito.when(k8sClient.services().inAnyNamespace().list().getItems())
                    .thenReturn(services);
            Mockito.when(k8sClient.services().inNamespace("default").list().getItems())
                    .thenReturn(services);
            ServiceResource<Service> hasSvcRes =
                    Mockito.mock(ServiceResource.class);
            Mockito.when(k8sClient.services().inNamespace("default").withName("svcname"))
                    .thenReturn(hasSvcRes);
            Mockito.when(k8sClient.services().inNamespace("default").withName("svcname")
                            .get())
                    .thenReturn(svc);
            ServiceResource<Service> nosvcRes =
                    Mockito.mock(ServiceResource.class);
            Mockito.when(k8sClient.services().inNamespace("default").withName("nosvc"))
                    .thenReturn(nosvcRes);
            Mockito.when(k8sClient.services().inNamespace("default").withName("nosvc").get())
                    .thenReturn(null);
            Mockito.when(k8sClient.services().inNamespace("default").resource(any(
                            Service.class
                    ))).thenReturn(nosvcRes);
            Mockito.when(nosvcRes.createOrReplace()).thenReturn(null);

            //处理deployment



        } catch (Exception e) {
            e.printStackTrace();
        }
        return basicModel;
    }

    
    @Test
    void getAllNodeInfo() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();

        k8sClusterService.getAllNodeInfo(basicModel);
        k8sClusterService.getAllNodeInfo(basicModel,CnapCloudMgmtConstants.TEST_ADMIN,"labelkey","labelValue");

    }

    @Test
    void testGetAllNodeInfo() {
    }

    @Test
    void editK8sClusterNodeInfo() throws Exception {

        getMockBasicModel();
        NodeModel nodeModel = new NodeModel();
        nodeModel.setNodeName("nodename");
        nodeModel.setNodeLabelMap(new HashMap<>());
        nodeModel.getNodeLabelMap().put("a","b");
        nodeModel.setK8sClusterIp("1.1.1.1");

        k8sClusterService.editK8sClusterNodeInfo(nodeModel);
    }

    @Test
    void getK8sClusterMasterNodeNum() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getK8sClusterMasterNodeNum("aliyun","clustername");


    }

    @Test
    void getK8sClusterNodeNum() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getK8sClusterNodeNum("aliyun","clustername");
    }

    @Test
    void getK8sClusterSvcNum() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel =  getMockBasicModel();
        int result = k8sClusterService.getK8sClusterSvcNum(
                "aliyun","clustername");
        result++;
    }

    @Test
    void getK8sClusterNodeInfosByConditions() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel =  getMockBasicModel();
        k8sClusterService.getK8sClusterNodeInfosByConditions(
                "aliyun","clustername", "node",10,1);
        k8sClusterService.getK8sClusterNodeInfosByConditions(
                "aliyun","clustername", "",10,1
        );
        k8sClusterService.getK8sClusterNodeInfosByConditions(
                "aliyun","clustername",
                "nodename","node",10,1
        );
        k8sClusterService.getK8sClusterNodeInfosByConditions(
                "aliyun","clustername",
                "nodename","node",10,1,
                "labelkey","labelValue");
    }

    @Test
    void testGetK8sClusterNodeInfosByConditions() {
    }

    @Test
    void testGetK8sClusterNodeInfosByConditions1() {
    }

    @Test
    void getPodMetricsByNs() {
        k8sClusterService.getPodMetricsByNs(null,null);

        KubernetesClient k8sClient = Mockito.mock(KubernetesClient.class);
        MetricAPIGroupDSL mockTop = Mockito.mock(MetricAPIGroupDSL.class);
        Mockito.when(k8sClient.top()).thenReturn(mockTop);
        PodMetricOperationsImpl podMetricOperations = Mockito.mock(PodMetricOperationsImpl.class);
        Mockito.when(k8sClient.top().pods()).thenReturn(podMetricOperations);
        PodMetricOperation mcokpodTopWithNs = Mockito.mock(PodMetricOperation.class);
        Mockito.when(k8sClient.top().pods().inNamespace(Mockito.anyString()))
                        .thenReturn(null);

        k8sClusterService.getPodMetricsByNs(CnapCloudMgmtConstants.TEST_ADMIN,
                k8sClient);
    }

    @Test
    void getAllNameSpaceNames() {

        K8sClusterBasicModel k8sClusterBasicModel = getMockBasicModel();
        List<String> nslist = k8sClusterService.getAllNameSpaceNames(k8sClusterBasicModel);

    }

    @Test
    void getAllPodsInfo() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getAllPodsInfo(basicModel,null);
        k8sClusterService.getAllPodsInfo(basicModel,"default");

    }

    @Test
    void testGetAllPodsInfo() {
    }

    @Test
    void getReplicaSetByName() {
    }

    @Test
    void getDeploymentByName() {
    }

    @Test
    void getStatefulSetByName() {
    }

    @Test
    void getDaemonSetByName() {
    }

    @Test
    void getOwnerReference() {
        //other invoke
        KubernetesClient k8sClient = new DefaultKubernetesClient();
        List<OwnerReference> ownerReferences = new ArrayList<>();
        OwnerReference ownerReference = new OwnerReference();
        ownerReference.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DEPLOYMENT);
        ownerReferences.add(ownerReference);
        k8sClusterService.getOwnerReference(CnapCloudMgmtConstants.TEST_ADMIN,k8sClient,ownerReferences);

    }

    @Test
    void getOwnerReference01() {
        //other invoke
        KubernetesClient k8sClient = new DefaultKubernetesClient();
        List<OwnerReference> ownerReferences = new ArrayList<>();
        OwnerReference ownerReference = new OwnerReference();
        ownerReference.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_STATEFULSET);
        ownerReferences.add(ownerReference);
        k8sClusterService.getOwnerReference(CnapCloudMgmtConstants.TEST_ADMIN,k8sClient,ownerReferences);

    }

    @Test
    void getOwnerReference02() {
        //other invoke
        KubernetesClient k8sClient = new DefaultKubernetesClient();
        List<OwnerReference> ownerReferences = new ArrayList<>();
        OwnerReference ownerReference = new OwnerReference();
        ownerReference.setKind(CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DAEMONSET);
        ownerReferences.add(ownerReference);
        k8sClusterService.getOwnerReference(CnapCloudMgmtConstants.TEST_ADMIN,k8sClient,ownerReferences);

    }

    @Test
    void delK8sClusterNode() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setVmModels(new ArrayList<>());
        VmModel masterModel = new VmModel();
        masterModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_MASTER);
        VmModel workerModel = new VmModel();
        workerModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_WORKER);
        k8sClusterBasicModel.getVmModels().add(masterModel);
        k8sClusterBasicModel.getVmModels().add(workerModel);
        masterModel.setInnerIp("2.2.2.2");
        workerModel.setInnerIp("3.3.3.3");

        Mockito.when(multiCloudMgmtService.getK8sClientByIp("1.1.1.1"))
                        .thenReturn(k8sClusterBasicModel);
        k8sClusterService.delK8sClusterNode("1.1.1.1","node-3.3.3.3");
    }

    @Test
    void testDelK8sClusterNode() {
    }

    @Test
    void getK8sClusterNameSpaces() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getK8sClusterNameSpaces("aliyun","clustername",
                "sv",100,1);

    }

    @Test
    void getAllNameSpaceInfo() throws ParseException {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getAllNameSpaceInfo(basicModel);
    }

    @Test
    void getAllSvcNum() {
    }

    @Test
    void getSvcInfo() throws ParseException {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getAllSvcInfo(basicModel,"default");
        k8sClusterService.getAllSvcInfo(basicModel,null);
        k8sClusterService.getSvcInfo(basicModel,"default","svcname");

    }

    @Test
    void getAllSvcInfo() throws Exception {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getAllSvcNum("aliyun","clustername");
    }

    @Test
    void addK8sClusterNameSpaces() throws Exception {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        NameSpaceModel nameSpaceModel = new NameSpaceModel();
        nameSpaceModel.setCloudVendor("aliyun");
        nameSpaceModel.setK8sClusterName("clustername");
        nameSpaceModel.setNameSpaceName("nsnewname");
        nameSpaceModel.setDescribe("descirbe");
        k8sClusterService.addK8sClusterNameSpaces(nameSpaceModel);

    }

    @Test
    void delK8sClusterNameSpaces() throws Exception {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.delK8sClusterNameSpaces("aliyun","clustername","newns");
    }

    @Test
    void getK8sClusterWorkLoads() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                .getK8sClientByCloudVendorAndName("aliyun",
                        CnapCloudMgmtConstants.TEST_ADMIN))
                        .thenReturn(k8sClusterBasicModel);
        k8sClusterService.getK8sClusterWorkLoads("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"status","",100,1);
    }

    @Test
    void getAllWorkLoads() throws ParseException {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getAllWorkLoads(basicModel,null);
        k8sClusterService.getAllWorkLoads(basicModel,"default");

    }

    @Test
    void editK8sClusterWorkLoadByYaml() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService.getK8sClientByCloudVendorAndName("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN)).thenReturn(k8sClusterBasicModel);
        k8sClusterService.editK8sClusterWorkLoadByYaml("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"");
        k8sClusterService.editK8sClusterWorkLoadByYaml("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"11");
    }

    @Test
    void addK8sClusterWorkLoad() throws Exception {
        /**
         *             if (k8sClient.apps().deployments().inNamespace(newDeployment.getMetadata().getNamespace())
         *                     .resource((Deployment) kubernetesResource) != null) {
         */

        String deploymentYaml = "apiVersion: apps/v1\n" +
                "kind: Deployment\n" +
                "metadata:\n" +
                "  annotations:\n" +
                "    deployment.kubernetes.io/revision: \"1\"\n" +
                "  creationTimestamp: \"2022-12-01T08:52:44Z\"\n" +
                "  generation: 1\n" +
                "  labels:\n" +
                "    app: cnap-algorithm\n" +
                "  name: nodep\n" +
                "  namespace: default\n" +
                "  resourceVersion: \"4237265\"\n" +
                "  uid: c383d9de-eaf4-4ebc-bb06-436cdc73f3c6\n" +
                "spec:\n" +
                "  progressDeadlineSeconds: 600\n" +
                "  replicas: 1\n" +
                "  revisionHistoryLimit: 10\n" +
                "  selector:\n" +
                "    matchLabels:\n" +
                "      app: cnap-algorithm\n" +
                "  strategy:\n" +
                "    rollingUpdate:\n" +
                "      maxSurge: 1\n" +
                "      maxUnavailable: 1\n" +
                "    type: RollingUpdate\n" +
                "  template:\n" +
                "    metadata:\n" +
                "      creationTimestamp: null\n" +
                "      labels:\n" +
                "        app: cnap-algorithm\n" +
                "    spec:\n" +
                "      containers:\n" +
                "      - env:\n" +
                "        - name: DB_URL\n" +
                "          value: cnap-auth-mysql-svc.default.svc.cluster.local:3306\n" +
                "        - name: USER\n" +
                "          value: root\n" +
                "        - name: PASSWORD\n" +
                "          value: db!@#123Handle\n" +
                "        - name: DATABASE\n" +
                "          value: cnap_schedule\n" +
                "        - name: SPECS_DB_URL\n" +
                "          value: cnap-auth-mysql-svc.default.svc.cluster.local:3306\n" +
                "        - name: SPECS_USER\n" +
                "          value: root\n" +
                "        - name: SPECS_PASSWORD\n" +
                "          value: db!@#123Handle\n" +
                "        - name: SPECS_DATABASE\n" +
                "          value: tsn_simulation\n" +
                "        image: 172.25.4.11/public/cnap-algorithm-be:cnap-v1.2-1201-2207344\n" +
                "        imagePullPolicy: IfNotPresent\n" +
                "        name: cnap-algorithm\n" +
                "        ports:\n" +
                "        - containerPort: 8808\n" +
                "          protocol: TCP\n" +
                "        resources: {}\n" +
                "        terminationMessagePath: /dev/termination-log\n" +
                "        terminationMessagePolicy: File\n" +
                "      dnsPolicy: ClusterFirst\n" +
                "      restartPolicy: Always\n" +
                "      schedulerName: default-scheduler\n" +
                "      securityContext: {}\n" +
                "      terminationGracePeriodSeconds: 30\n";
        String statfulYaml = "apiVersion: apps/v1\n" +
                "kind: StatfulSet\n" +
                "metadata:\n" +
                "  annotations:\n" +
                "    deployment.kubernetes.io/revision: \"1\"\n" +
                "  creationTimestamp: \"2022-12-01T08:52:44Z\"\n" +
                "  generation: 1\n" +
                "  labels:\n" +
                "    app: cnap-algorithm\n" +
                "  name: cnap-algorithm\n" +
                "  namespace: default\n" +
                "  resourceVersion: \"4237265\"\n" +
                "  uid: c383d9de-eaf4-4ebc-bb06-436cdc73f3c6\n" +
                "spec:\n" +
                "  progressDeadlineSeconds: 600\n" +
                "  replicas: 1\n" +
                "  revisionHistoryLimit: 10\n" +
                "  selector:\n" +
                "    matchLabels:\n" +
                "      app: cnap-algorithm\n" +
                "  strategy:\n" +
                "    rollingUpdate:\n" +
                "      maxSurge: 1\n" +
                "      maxUnavailable: 1\n" +
                "    type: RollingUpdate\n" +
                "  template:\n" +
                "    metadata:\n" +
                "      creationTimestamp: null\n" +
                "      labels:\n" +
                "        app: cnap-algorithm\n" +
                "    spec:\n" +
                "      containers:\n" +
                "      - env:\n" +
                "        - name: DB_URL\n" +
                "          value: cnap-auth-mysql-svc.default.svc.cluster.local:3306\n" +
                "        - name: USER\n" +
                "          value: root\n" +
                "        - name: PASSWORD\n" +
                "          value: db!@#123Handle\n" +
                "        - name: DATABASE\n" +
                "          value: cnap_schedule\n" +
                "        - name: SPECS_DB_URL\n" +
                "          value: cnap-auth-mysql-svc.default.svc.cluster.local:3306\n" +
                "        - name: SPECS_USER\n" +
                "          value: root\n" +
                "        - name: SPECS_PASSWORD\n" +
                "          value: db!@#123Handle\n" +
                "        - name: SPECS_DATABASE\n" +
                "          value: tsn_simulation\n" +
                "        image: 172.25.4.11/public/cnap-algorithm-be:cnap-v1.2-1201-2207344\n" +
                "        imagePullPolicy: IfNotPresent\n" +
                "        name: cnap-algorithm\n" +
                "        ports:\n" +
                "        - containerPort: 8808\n" +
                "          protocol: TCP\n" +
                "        resources: {}\n" +
                "        terminationMessagePath: /dev/termination-log\n" +
                "        terminationMessagePolicy: File\n" +
                "      dnsPolicy: ClusterFirst\n" +
                "      restartPolicy: Always\n" +
                "      schedulerName: default-scheduler\n" +
                "      securityContext: {}\n" +
                "      terminationGracePeriodSeconds: 30\n";
        getMockBasicModel();
        k8sClusterService.addK8sClusterWorkLoad("aliyun","clustername",deploymentYaml);
        k8sClusterService.addK8sClusterWorkLoad("aliyun","clustername",statfulYaml);

    }

    @Test
    void editK8sClusterWorkLoadReplicas() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        //k8sClusterService.editK8sClusterWorkLoadReplicas("aliyun","clustername",
           //     "deployment","default",CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DEPLOYMENT,2);
        k8sClusterService.editK8sClusterWorkLoadReplicas("aliyun","clustername",
                "statefulset123","default","123getK8sClusterPods",2);

    }

    @Test
    void editK8sClusterWorkLoadContainers() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService.getK8sClientByCloudVendorAndName("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN)).thenReturn(k8sClusterBasicModel);
        List<ContainerModel> containerModels = new ArrayList<>();
        ContainerModel containerModel = new ContainerModel();
        containerModel.setContainerName("containerName");
        containerModel.setContainerImage("image");
        containerModels.add(containerModel);
        k8sClusterService.editK8sClusterWorkLoadContainers("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"work","ns",
                CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_DEPLOYMENT,containerModels);
        k8sClusterService.editK8sClusterWorkLoadContainers("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"work","ns",
                CnapCloudMgmtConstants.CNAP_WORKLOAD_KIND_STATEFULSET,containerModels);

    }

    @Test
    void delK8sClusterWorkLoad() {
    }

    @Test
    void getK8sClusterPods() throws Exception {

        getMockBasicModel();
        k8sClusterService.getK8sClusterPods("aliyun","clustername","pod","default",
                "","",100,1);
        k8sClusterService.getK8sClusterPods("aliyun","clustername","pod",null,
                "","",100,1);
    }

    @Test
    void testGetK8sClusterPods() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                .getK8sClientByCloudVendorAndName("aliyun",CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.getK8sClusterPods("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,
                "status","","","",CnapCloudMgmtConstants.TEST_ADMIN,100,1);
        k8sClusterService.getK8sClusterPods("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,
                "status","","","","",100,1);
        k8sClusterService.getK8sClusterPods("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,
                "","","","workerLoadName","",100,1);
        k8sClusterService.getK8sClusterPods("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,
                "","","svcName","","",100,1);
    }

    @Test
    void getSvcModelByName() {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getSvcModelByName(basicModel,"default","svcname");
    }

    @Test
    void delK8sClusterPod() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.delK8sClusterPod("aliyun","clustername",
                "default","delpod");
    }

    @Test
    void getK8sClusterPodContainers() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getK8sClusterPodContainers("aliyun","clustername",
                "default","podname");

    }

    @Test
    void testGetAllNodeInfo1() {
    }

    @Test
    void testGetAllNodeInfo2() {
    }

    @Test
    void testEditK8sClusterNodeInfo() {
    }

    @Test
    void testGetK8sClusterMasterNodeNum() {
    }

    @Test
    void testGetK8sClusterNodeNum() {
    }

    @Test
    void testGetK8sClusterSvcNum() {
    }

    @Test
    void testGetK8sClusterNodeInfosByConditions2() {
    }

    @Test
    void testGetK8sClusterNodeInfosByConditions3() {
    }

    @Test
    void testGetK8sClusterNodeInfosByConditions4() {
    }

    @Test
    void testGetPodMetricsByNs() {
    }

    @Test
    void testGetAllNameSpaceNames() {
    }

    @Test
    void testGetAllPodsInfo1() {
    }

    @Test
    void testGetAllPodsInfo2() {
    }

    @Test
    void testGetReplicaSetByName() {
        //other invoke
    }

    @Test
    void testGetDeploymentByName() {
        //other invoke
    }

    @Test
    void testGetStatefulSetByName() {
        //other invoke
    }

    @Test
    void testGetDaemonSetByName() {
        //other invoke
    }

    @Test
    void testGetOwnerReference() {
    }

    @Test
    void testDelK8sClusterNode1() {
    }

    @Test
    void testDelK8sClusterNode2() {
    }

    @Test
    void testGetK8sClusterNameSpaces() {
    }

    @Test
    void testGetAllNameSpaceInfo() {
    }

    @Test
    void testGetAllSvcNum() {
    }

    @Test
    void testGetSvcInfo() {
    }

    @Test
    void testGetAllSvcInfo() {
    }

    @Test
    void testAddK8sClusterNameSpaces() {
    }

    @Test
    void testDelK8sClusterNameSpaces() {
    }

    @Test
    void testGetK8sClusterWorkLoads() {
    }

    @Test
    void testGetAllWorkLoads() {
    }

    @Test
    void testEditK8sClusterWorkLoadByYaml() {
    }

    @Test
    void testAddK8sClusterWorkLoad() {
    }

    @Test
    void testEditK8sClusterWorkLoadReplicas() {
    }

    @Test
    void testEditK8sClusterWorkLoadContainers() {
    }

    @Test
    void testDelK8sClusterWorkLoad() {
    }

    @Test
    void testGetK8sClusterPods1() {
    }

    @Test
    void testGetK8sClusterPods2() {
    }

    @Test
    void testGetSvcModelByName() {
    }

    @Test
    void testDelK8sClusterPod() {
    }

    @Test
    void testGetK8sClusterPodContainers() {
    }

    @Test
    void getK8sClusterSvcs() throws Exception {

        K8sClusterBasicModel basicModel = getMockBasicModel();
        k8sClusterService.getK8sClusterSvcs("aliyun","clustername",
                "svc",100,1);
    }

    @Test
    void addK8sClusterSvc() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                        .getK8sClientByCloudVendorAndName("aliyun",CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.addK8sClusterSvc("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,"");


    }

    @Test
    void editK8sClusterSvc() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                        .getK8sClientByCloudVendorAndName("aliyun",CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        List<KeyValueModel> selectorModels = new ArrayList<>();
        KeyValueModel keyValueModel = new KeyValueModel();
        selectorModels.add(keyValueModel) ;
        keyValueModel.setValue("app");
        keyValueModel.setKey("app");
        k8sClusterService.editK8sClusterSvc("aliyun",CnapCloudMgmtConstants.TEST_ADMIN,
                "ns","nodePort","svcName",
                8080,8080,8080,selectorModels);
        //todo
    }

    @Test
    void delK8sClusterSvc() {
        //todo
    }

    @Test
    void editK8sClusterSvcByYaml() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);

        Mockito.when(multiCloudMgmtService.getK8sClientByCloudVendorAndName("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN)).thenReturn(k8sClusterBasicModel);
        k8sClusterService.editK8sClusterSvcByYaml("aliyun",
                CnapCloudMgmtConstants.TEST_ADMIN,"ns","svcName","");
        //todo
    }

    @Test
    void getSvcMonitorInfo() throws Exception {
        K8sClusterBasicModel basicModel = getMockBasicModel();
        List<SvcMonitorModel> svcMonitorModels = new ArrayList<>();
        SvcMonitorModel svcMonitorModel = new SvcMonitorModel();
        svcMonitorModel.setSeclectorMap(new HashMap<>());
        svcMonitorModel.getSeclectorMap().put("app","svc1");
        svcMonitorModels.add(svcMonitorModel);
        k8sClusterService.getSvcMonitorInfo("aliyun","clustername",svcMonitorModels);

    }

    @Test
    void getK8sClusterEvents() {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                        .getK8sClusterIpByName(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn("1.1.1.1");
        Mockito.when(k8sClusterEventsMapper.countK8sClusterEvents(Mockito.anyString(),Mockito.anyString(),
                Mockito.any(LocalDateTime.class),Mockito.anyString(),null,null)).thenReturn(1);
        k8sClusterService.getK8sClusterEvents(CnapCloudMgmtConstants.TEST_ADMIN,CnapCloudMgmtConstants.TEST_ADMIN,
                "2022-11-11 11:11:11","reason",100,1);

    }

    @Test
    void getSvcPodSpark() throws ParseException {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                        .getK8sClusterInfoByName(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.getSvcPodSpark(CnapCloudMgmtConstants.TEST_ADMIN,"ns","svcname");
    }

    @Test
    void getSvcPodFlinks() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService
                        .getK8sClusterInfoByName(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.getSvcPodFlinks(CnapCloudMgmtConstants.TEST_ADMIN,"ns","svcname");

        Mockito.when(multiCloudMgmtService
                        .getK8sClientByIp("1.1.1.1"))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.getUtListPods("1.1.1.1");

    }

    @Test
    void getK8sClusterInnerIp() {
        k8sClusterService.getK8sClusterInnerIp(null);
    }

    @Test
    void addNodeLabel() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterService.delK8sClusterNode(k8sClusterBasicModel,"nodeName");
    }

    @Test
    void testAddNodeLabel() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sStatus(K8sStatusEnum.Running.getChineseDescribe());

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.addNodeLabel(CnapCloudMgmtConstants.TEST_ADMIN,"nodeName",
                "labelkey","labelValue");

        k8sClusterService.addNodeLabel(CnapCloudMgmtConstants.TEST_ADMIN,"nodeName",
                "labelkey","");
    }

    @Test
    void testAddNodeLabel01() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sStatus(K8sStatusEnum.Stop.getChineseDescribe());

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.addNodeLabel(CnapCloudMgmtConstants.TEST_ADMIN, "nodeName",
                "labelkey", "labelValue");
    }

    @Test
    void testAddNodeLabel02() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sStatus(K8sStatusEnum.Stop.getChineseDescribe());

        Mockito.when(multiCloudMgmtService.getK8sClientByIp(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.checkSvcExist(CnapCloudMgmtConstants.TEST_ADMIN);
    }

    @Test
    void testAddNodeLabel03() throws Exception {
        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sStatus(K8sStatusEnum.Running.getChineseDescribe());

        Mockito.when(multiCloudMgmtService.getK8sClientByIp(CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.checkSvcExist(CnapCloudMgmtConstants.TEST_ADMIN);
    }


    @Test
    void addK8sClusterNode() {
    }

    @Test
    void installCnapScheduleAgent() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);

        Mockito.when(multiCloudMgmtService.getK8sClientByIp("1.1.1.1"))
                .thenReturn(k8sClusterBasicModel);
        k8sClusterService.installCnapScheduleAgent("1.1.1.1",null);
    }
}